package com.mes.web.controller.biz;

import java.math.BigDecimal;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.mes.biz.domain.BizCustomer;
import com.mes.biz.domain.BizSupplier;
import com.mes.biz.service.BizCustomerService;
import com.mes.biz.service.BizSupplierService;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizProductInventoryBook;
import com.mes.biz.service.BizProductInventoryBookService;

/**
 * 库存台账Controller
 * @author hwg
 * @email hwg
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/biz/bizProductInventoryBook")
public class BizProductInventoryBookController extends BaseController {

    @Autowired
    private BizProductInventoryBookService bizProductInventoryBookService;
    @Autowired
    private BizCustomerService bizCustomerService;
    @Autowired
    private BizSupplierService bizSupplierService;

    /**
     * 查询库存台账列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizProductInventoryBook bizProductInventoryBook, HttpServletRequest request, HttpServletResponse response) {
        bizProductInventoryBook.setPage(new PageDomain(request, response));
        PageInfo<BizProductInventoryBook> page = bizProductInventoryBookService.findPage(bizProductInventoryBook);
        for (int i = 0; i < page.getList().size(); i++) {
           if (StringUtils.isNotEmpty(page.getList().get(i).getCustomer())){
               BizCustomer bizCustomer = bizCustomerService.get(page.getList().get(i).getCustomer());
               if (bizCustomer !=null){
                   page.getList().get(i).setSupplierName(bizCustomer.getCustomerName());
               }else {
                   BizSupplier bizSupplier = bizSupplierService.getByCode(page.getList().get(i).getCustomer());
                   if (bizSupplier !=null){
                       page.getList().get(i).setSupplierName(bizSupplier.getSupplierName());
                   }
               }
           }
        }
        return R.data(page);
    }

    /**
     * 获取库存台账详细信息
     */
    @GetMapping(value = "/{id}")
    public R<BizProductInventoryBook> detail(@PathVariable("id") String id) {
        return R.data(bizProductInventoryBookService.get(id));
    }

    /**
     * 新增库存台账
     */
    @Log(title = "库存台账", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizProductInventoryBook bizProductInventoryBook) {
        return R.status(bizProductInventoryBookService.save(bizProductInventoryBook));
    }

    /**
     * 修改库存台账
     */
    @Log(title = "库存台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizProductInventoryBook bizProductInventoryBook) {
        return R.status(bizProductInventoryBookService.save(bizProductInventoryBook));
    }

    /**
     * 删除库存台账
     */
    @Log(title = "库存台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizProductInventoryBookService.deleteBizProductInventoryBookByIds(ids));
    }


    /**
     * 导出库存台账列表
     */
    @Log(title = "库存台账", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizProductInventoryBook bizProductInventoryBook) {
        List<BizProductInventoryBook> list = bizProductInventoryBookService.findList(bizProductInventoryBook);
        ExcelUtil<BizProductInventoryBook> util = new ExcelUtil<BizProductInventoryBook>(BizProductInventoryBook.class);
        return util.exportExcel(list, "库存台账数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
